package com.haisen.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.haisen.core.utils.PageResultSet;
import com.haisen.dataobject.dto.VerifyPubSubDTO;
import com.haisen.dataobject.entity.JobNo;
import com.haisen.dataobject.entity.VerifyPubSub;
import com.haisen.dataobject.mapper.base.JobNoMapper;
import com.haisen.dataobject.mapper.base.VerifyPubSubMapper;
import com.haisen.dataobject.query.VerifyPubSubQuery;
import com.haisen.service.VerifyPubSubService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.weekend.Weekend;
import tk.mybatis.mapper.weekend.WeekendCriteria;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class VerifyPubSubServiceImp implements VerifyPubSubService {


    @Autowired
    private VerifyPubSubMapper verifyPubSubMapper;
    @Autowired
    private JobNoMapper jobNoMapper;

   private List<JobNo> jobNoList = new ArrayList<>();
    @Override
    public void create(VerifyPubSub verifyPubSub) {
         Weekend<VerifyPubSub> example = Weekend.of(VerifyPubSub.class);
         WeekendCriteria<VerifyPubSub,Object> criteria = example.weekendCriteria();
         criteria.andEqualTo(VerifyPubSub::getSubscribe,verifyPubSub.getSubscribe());
        if (verifyPubSubMapper.selectByExample(example).size() == 0){
            verifyPubSubMapper.insertSelective(verifyPubSub);
        } else {
            verifyPubSubMapper.updateByExampleSelective(verifyPubSub,example);
        }
    }

    @Override
    public void update(VerifyPubSub verifyPubSub) {
        verifyPubSubMapper.updateByPrimaryKey(verifyPubSub);
    }

    @Override
    public void delete(Long[] ids) {
        Weekend<VerifyPubSub> example = Weekend.of(VerifyPubSub.class);
        WeekendCriteria<VerifyPubSub,Object> criteria = example.weekendCriteria();
        criteria.andIn(VerifyPubSub::getId, Arrays.asList(ids));
        verifyPubSubMapper.deleteByExample(example);
    }

    @Override
    public List<VerifyPubSub> listAll() {
        return verifyPubSubMapper.selectAll();
    }

    //页面设计应该是用户...订阅...
    @Override
    public PageResultSet<VerifyPubSubDTO> findByPage(VerifyPubSubQuery verifyPubSubQuery) {
          //加载一次工号信息，避免重复查询
          jobNoList = jobNoMapper.selectAll();
        if (!StringUtils.isEmpty(verifyPubSubQuery.getOrderBy())){
            PageHelper.orderBy(verifyPubSubQuery.getOrderBy());
        }
          PageHelper.offsetPage(verifyPubSubQuery.getOffset(),verifyPubSubQuery.getLimit());
         Weekend<VerifyPubSub> example = Weekend.of(VerifyPubSub.class);
         WeekendCriteria<VerifyPubSub,Object> criteria = example.weekendCriteria();
         /*if (!StringUtils.isEmpty(verifyPubSubQuery.getPublish())){
             //页面搜索的文字转换为id,设计怪异
                List<Long>  publishIds = jobNoList.stream().filter(o -> {
                    //名称包含
                     if (o.getChineseNo().indexOf(verifyPubSubQuery.getPublish()) !=-1 ) return true;
                     else return false;
                 }).map(o -> {
                     return o.getId();
                 }).collect(Collectors.toList());
                //如果是两个集合，求工号中的ids和veripubsub中的交集，一条sql搞不定的
                //if()
             //todo:id和名称转换，需要页面处理下
             criteria.andEqualTo(VerifyPubSub::getPublishIds,verifyPubSubQuery.getPublish());
         }*/

         if (!StringUtils.isEmpty(verifyPubSubQuery.getSubscribe())){
             criteria.andEqualTo(VerifyPubSub::getSubscribe,verifyPubSubQuery.getSubscribe());
         }

        List<VerifyPubSubDTO> verifyPubSubDTOList = verifyPubSubMapper.selectByExample(example)
                 .stream().map( o -> {
              return new VerifyPubSubDTO(o.getId(),o.getSubscribe(),o.getSubscribeName(),o.getPublishIds(),getPubliserNames(o.getPublishIds()),"all");
         }).collect(Collectors.toList());
         PageResultSet<VerifyPubSubDTO> resultSet = new PageResultSet<>();
         resultSet.setRows(verifyPubSubDTOList);
         resultSet.setTotal(verifyPubSubMapper.selectCountByExample(example));
        return resultSet;
    }

    @Override
    public List<String> getSubByPubId(Long pubId) {
         List<VerifyPubSub> verifyPubSubList = verifyPubSubMapper.selectAll();
        return  verifyPubSubList.stream().filter(o ->{
             if (o.getPublishIdList().contains(pubId)) return true;
             else return false;
         }).map( o-> {
             return o.getSubscribe();
         }).collect(Collectors.toList());
    }


    public String getPubliserNames(String ids){
//        log.info("ids={}",ids);
         String[] strings = ids.split(",");

          List<Long> idList = new ArrayList<>();
          for(String s : strings){
              idList.add(Long.valueOf(s));
          }
        StringBuilder sb = new StringBuilder();
        jobNoList.stream().filter( o -> {
            if (idList.contains(o.getId())) return true;
            else return false;
        }).map(o -> {
            sb.append(o.getChineseNo());
            sb.append(",");
            return o.getChineseNo();
        }).collect(Collectors.toList());

        if (sb.length() > 0) {
            sb.deleteCharAt(sb.length() - 1);
        }
        return sb.toString();
    }

}
